package com.chain.qs.modules.biz.dp.handler;

import com.google.common.collect.Maps;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 设备历史记录处理抽像类
 */
public abstract class AbstractDPHandler implements DPHandler {

    public static HashMap<String,AbstractDPHandler> dpHandlerMap = Maps.newHashMap();

    public HashMap<String,Map> osdMap = new HashMap<String,Map>();

    //officecode
    protected String officeCode;
    //站组id
    protected String sgroupId;
    //设备id
    protected String deviceId;


    public AbstractDPHandler() {
        dpHandlerMap.put(getClass().getSimpleName(),this);
    }

    public void updateCache(Map<String, Object> params) {
        ConcurrentHashMap<String, Object> cacheMap = new ConcurrentHashMap();
        String osd = officeCode+"@"+sgroupId+"@"+deviceId;
        if ( osd!= null && !osdMap.containsKey(osd)){
            osdMap.put(osd,new HashMap<String,Object>());
        }
        for (Map.Entry<String, Object> entry : params.entrySet()) {
            cacheMap.put(entry.getKey(), entry.getValue());
        }
        osdMap.get(osd).put("cacheMap",cacheMap);
        osdMap.get(osd).put("isNewDataCache",true); //是否是新数据
        osdMap.get(osd).put("isWarning",false); //设备信息是否变化

    }

    public void saveDataToDB() {
        for (Map.Entry<String,Map> entry : osdMap.entrySet()) {
            if ((boolean)entry.getValue().get("isNewDataCache") && (boolean)entry.getValue().get("isWarning")){
                String [] index = entry.getKey().split("@");
                officeCode = index[0];
                sgroupId = index[1];
                deviceId = index[2];
                saveData((ConcurrentHashMap<String, Object>)(entry.getValue().get("cacheMap")));
                entry.getValue().put("isNewDataCache",false);
                entry.getValue().put("isWarning",false);
            }
        }
    }

    /**
     *  该方法专门为定时器调用
     */
    public void fixedTimeSaveDataToDB() {
        for (Map.Entry<String,Map> entry : osdMap.entrySet()) {
            if ((boolean)entry.getValue().get("isNewDataCache")){
                String [] index = entry.getKey().split("@");
                officeCode = index[0];
                sgroupId = index[1];
                deviceId = index[2];
                saveData((ConcurrentHashMap<String, Object>)(entry.getValue().get("cacheMap")));
                entry.getValue().put("isNewDataCache",false);
                entry.getValue().put("isWarning",false);
            }
        }
    }

    public boolean isWarning(String osdKey) {
        if(osdMap.containsKey(osdKey)){
            return (boolean)(osdMap.get(osdKey).get("isWarning"));
        }
        return true;
    }

    public void setWarning(String osdKey , boolean warning) {
        if(osdMap.containsKey(osdKey)){
            osdMap.get(osdKey).put("isWarning",warning);
        }
    }

    public abstract void saveData(Map<String, Object> params);

    public void setDeviceId(String deviceId) {
        this.deviceId = deviceId;
    }

    public void setOfficeCode(String officeCode) {
        this.officeCode = officeCode;
    }

    public void setSgroupId(String sgroupId) {
        this.sgroupId = sgroupId;
    }
}
